<?php
namespace App\Models;

class ProjectNoticeLog extends Base {

    public function initialize() {
        parent::initialize();
    }

    public function getSource() {
        return "project_notice_log";
    }

    private function t() {
        return __CLASS__;
    }

    /**
     * 根据条件统计短信营销总数
     *
     * @param array $where 搜索条件
     *
     * @return int|false
     */
    public function total($where) {
        $query = $this->select('COUNT(*) AS cnt')->from($this->t());
        if(is_array($where) && count($where) > 0) {
            if(isset($where['mobile'])) {
                $query->andWhere('mobile=:mobile:', ['mobile' => $where['mobile']]);
            }
            if(isset($where['status'])) {
                $query->andWhere('status=:status:', ['status' => $where['status']]);
            }
            if(isset($where['project_id'])) {
                $query->andWhere('project_id=:project_id:', ['project_id' => $where['project_id']]);
            }
            if(isset($where['start_date'])) {
                $query->andWhere('send_at>=:start:', ['start' => date("Y-m-d H:i:s", strtotime($where['start_date']))]);
            }
            if(isset($where['end_date'])) {
                $query->andWhere('send_at<:end:', ['end' => date("Y-m-d H:i:s", strtotime($where['end_date']) + 24 * 60 * 60)]);
            }
        }
        try {
            $ret = $query->getQuery()->execute();
            if($ret != false) {
                $ret = $ret->toArray();
                if(is_array($ret) && count($ret) > 0 && isset($ret[0]['cnt'])) {
                    return intval($ret[0]['cnt']);
                }
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 根据条件获取短信营销信息
     *
     * @param int   $offset 起始位置
     * @param int   $limit 限制条数
     * @param array $where 搜索条件
     *
     * @return array|false
     */
    public function gets($where, $offset, $limit) {
        $query = $this->select('*')->from($this->t());
        if(is_array($where) && count($where) > 0) {
            if(isset($where['mobile'])) {
                $query->andWhere('mobile=:mobile:', ['mobile' => $where['mobile']]);
            }
            if(isset($where['status'])) {
                $query->andWhere('status=:status:', ['status' => $where['status']]);
            }
            if(isset($where['project_id'])) {
                $query->andWhere('project_id=:project_id:', ['project_id' => $where['project_id']]);
            }
            if(isset($where['start_date'])) {
                $query->andWhere('send_at>=:start:', ['start' => date("Y-m-d H:i:s", strtotime($where['start_date']))]);
            }
            if(isset($where['end_date'])) {
                $query->andWhere('send_at<:end:', ['end' => date("Y-m-d H:i:s", strtotime($where['end_date']) + 24 * 60 * 60)]);
            }
        }
        try {
            $ret = $query->orderBy('send_at DESC')->limit($limit, $offset)->getQuery()->execute();
            if($ret !== false) {
                return $ret->toArray();
            }
        } catch(Exception $ex) {
            $this->errorLog('SELECT', $query->getPhql(), $ex->getMessage(), __FILE__, __LINE__);
        }
        return false;
    }

    /**
     * 根据id值获取短信营销信息
     *
     * @param int $id 短信id值
     *
     * @return array|false
     */
    public function get($id) {
        if($id == '' || !is_numeric($id)) {
            return false;
        }
        //@formatter:off
        $ret = $this->select('*')
            ->from($this->t())
            ->where('id =:id:', array('id' => $id))
            ->limit(1)
            ->getQuery()
            ->execute();
        //@formatter:on
        if($ret !== false) {
            $ret = $ret->toArray();
            if(count($ret) > 0) {
                return $ret[0];
            }
        }
        return false;
    }
}

